package com.galacticconversion.command;

import com.galacticconversion.model.DataStorage;
import com.galacticconversion.util.RomanNumberUtil;

/**
 * 处理查询金属价值命令 (如 "how many Credits is glob prok Silver ?")
 */
public class MetalValueCommand implements Command {
    private final DataStorage dataStorage;

    public MetalValueCommand(DataStorage dataStorage) {
        this.dataStorage = dataStorage;
    }

    @Override
    public void execute(String input) {
        String query = input.substring("how many Credits is ".length(), input.length() - 1).trim();
        String[] parts = query.split(" ");
        String metal = parts[parts.length - 1];

        // 提取星际单位部分
        StringBuilder galacticUnits = new StringBuilder();
        for (int i = 0; i < parts.length - 1; i++) {
            galacticUnits.append(parts[i]).append(" ");
        }

        String roman = RomanNumberUtil.convertGalacticToRoman(galacticUnits.toString().trim(), dataStorage);
        int value = RomanNumberUtil.romanToDecimal(roman);

        if (value > 0 && dataStorage.getMetalPrices().containsKey(metal)) {
            double total = value * dataStorage.getMetalPrices().get(metal);
            System.out.println(query + " is " + (int) total + " Credits");
        } else {
            System.out.println("I have no idea what you are talking about");
        }
    }

}